package outbound

import (
	"bytes"
	"context"
	"encoding/hex"
	"github.com/v2pro/plz/countlog"
)

var mysqlGreeting []byte
var maxAllowedPacketResp []byte

func init() {
	mysqlGreeting, _ = hex.DecodeString("4e0000000a352e372e31362d6c6f67009f943f003663266e4a62520d00fff7530200ff8115000000000000000000003e5529546501277e7c430f1a006d7973716c5f6e61746976655f70617373776f726400")
	maxAllowedPacketResp, _ = hex.DecodeString(
		"0100000101" +
			"2a000002036465660000001440406d61785f616c6c6f7765645f7061636b6574000c3f001500000008a000000000" +
			"05000003fe00000200" +
			"09000004083637313038383634" +
			"05000005fe00000200")
}

func simulateMysql(ctx context.Context, request []byte) []byte {
	resp := _simulateMysql(ctx, request)
	if resp != nil {
		resp[3] = request[3] + 1
	}
	return resp
}

func _simulateMysql(ctx context.Context, request []byte) []byte {
	if bytes.Index(request, []byte("mysql_native_password")) != -1 {
		countlog.Debug("event!outbound.simulated_mysql",
			"ctx", ctx,
			"requestKeyword", "mysql_native_password",
			"content", request)
		return []byte{0x07, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00}
	}

	if bytes.Index(request, []byte("@@max_allowed_packet")) != -1 {
		countlog.Debug("event!outbound.simulated_mysql",
			"ctx", ctx,
			"requestKeyword", "@@max_allowed_packet",
			"content", request)
		return maxAllowedPacketResp
	}

	if bytes.Index(request, []byte("SET NAMES utf8")) != -1 {
		countlog.Debug("event!outbound.simulated_mysql",
			"requestKeyword", "SET NAMES utf8",
			"ctx", ctx,
			"content", request)
		return []byte{0x07, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00}
	}

	if bytes.Index(request, []byte("START TRANSACTION")) != -1 {
		countlog.Debug("event!outbound.simulated_mysql",
			"requestKeyword", "START TRANSACTION",
			"ctx", ctx,
			"content", request)
		return []byte{0x07, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00}
	}
	return nil
}
